import random
import datetime
from time import sleep

money=float(input("输入账户金额"))  #输入余额
shopping_cart=[]    #购物车
goods=[("apple",5),("banana",4),("orange",3)]#货架

# 优惠券配置
coupons = [
    ("apple", 0.8), ("banana", 0.9), ("orange", 0.7),
    ("apple", 0.85), ("banana", 0.75), ("orange", 0.6),
    ("apple", 0.7), ("banana", 0.8), ("orange", 0.9), ("apple", 0.6)
]
print("账户余额：",money)
print("您好，这里有10张优惠券：")
for i in coupons:
    print(i[0],"商品的",i[1]*10,"折优惠券")
print("请您抽一张优惠券")
print("抽取中\n...")
sleep(0.5)
print("....")
sleep(0.5)
print(".....")
sleep(0.5)
print("......")
sleep(0.5)
selected_coupon = random.choice(coupons)
print(f"恭喜您抽到{selected_coupon[0]}商品的{selected_coupon[1]*10}折优惠券")

while True:
    print("当前购物车:",[item['商品']for item in shopping_cart])
    action = input("\n请选择操作：\n[C] 继续购物\n[R] 退货\n[Q] 结账\n>>>").lower()
    if action == 'q':
        break
    elif action == 'c':
        print("\n当前货架商品：")
        for i in goods:
            discount = f"(可用{selected_coupon[1]*10}折折扣)"if i[0]==selected_coupon[0] else ""
            print(f"-{i[0].capitalize()}:原价￥{i[1]}{discount}")

        buy=input("请输入您要购买的商品名称：").lower()
        found=False

        for i in goods:
            if i[0].lower()==buy:
                found = True
                #计算折扣价格
                original_price=i[1]
                final_price=original_price*selected_coupon[1] if i[0]==selected_coupon[0] else original_price

                if money>=final_price:
                    money-=final_price
                    shopping_cart.append({
                        "商品":i[0],
                        "原价":original_price,
                        "现价":final_price,
                        "折扣":selected_coupon[1] if i[0]==selected_coupon[0] else None
                    })
                    print(f"恭喜，成功购买{i[0].capitalize()}！")
                    print(f"当前余额：￥{money:.2f}")
                else:print("\n余额不足，请选择其他商品！")
                break
        if not found:
            print("商品不存在！")
    elif action == 'r':
        if not shopping_cart:
            print("您的购物车为空")
            continue

        return_i=input("\n请输入要退回的商品名称").lower()
        returned=False

        for i in reversed(range(len(shopping_cart))):
            if shopping_cart[i]['商品'].lower()==return_i:
                money+=shopping_cart[i]['现价']
                print(f"\n已退回{shopping_cart[i]['商品'].capitalize()},返还金额：￥{shopping_cart[i]['现价']:.2f}")
                del shopping_cart[i]
                returned=True
                break

        if not returned:
            print("\n未找到商品！")

    else:
        print("\n无效操作，请重新输入！")

#打印小条
print("\n"+"="*40)
print("购物小条".center(40))
print("="*40)
print(f"时间：{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"优惠券：{selected_coupon[0].capitalize()}{selected_coupon[1]*10}折券")
print("-"*40)

total=0
for i in shopping_cart:
    price=f"￥{i['原价']}→￥{i['现价']:.2f}" if i['折扣'] else f"￥{i['原价']}"
    print(f"·{i['商品'].capitalize():<10}{price:>20}")
    total+=i['现价']

print("-"*40)
print(f"商品总数：{len(shopping_cart)}件")
print(f"消费合计{total:.2f}")
print(f"剩余金额{money:.2f}")
print("="*40)
print("谢谢惠顾，欢迎下次光临！\n")